using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class BodyScannerItem : BaseItem
	{
		public enum State
		{
			Game1 = 0,
			Game2 = 1
		}

		private State state;

		private Transform btnGame1;

		private Transform btnGame2;

		private BaseItem displayer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			btnGame1 = model.Find("BtnGame1");
			btnGame1.gameObject.layer = 15;
			btnGame1.SetLocalZ(-1E-05f);
			btnGame2 = model.Find("BtnGame2");
			btnGame2.gameObject.layer = 15;
			btnGame2.SetLocalZ(-1E-05f);
			SetState((State)data.state);
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag)
			{
				CheckScannerState();
			}
			return flag;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			CheckScannerState();
		}

		private void CheckScannerState()
		{
			if (displayer == null)
			{
				displayer = Singleton<Scene>.Instance.GetItemByScriptType(112);
			}
			if (displayer == null)
			{
				return;
			}
			if (childItems.Count > 0)
			{
				if (state == State.Game1)
				{
					((BodyScannerDisplayerItem)displayer).SetDisplayerType(1);
				}
				else
				{
					((BodyScannerDisplayerItem)displayer).SetDisplayerType(2);
				}
			}
			else
			{
				((BodyScannerDisplayerItem)displayer).SetDisplayerType(0);
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btnGame1)
				{
					SetState(State.Game1);
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				}
				else if (raycastHit2D.collider.transform == btnGame2)
				{
					SetState(State.Game2);
					Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
				}
			}
		}

		public override void OnChildDoThing(BaseItem item, ChildDoThingType type, object parms)
		{
			base.OnChildDoThing(item, type, parms);
			if (type != 0)
			{
				return;
			}
			BaseItem baseItem = (BaseItem)parms;
			if (!(baseItem != null))
			{
				return;
			}
			if (state == State.Game1)
			{
				if (baseItem.itemData.type == 10 && displayer != null)
				{
					SpriteRenderer component = baseItem.model.GetComponent<SpriteRenderer>();
					((BodyScannerDisplayerItem)displayer).PlayGame1Anim(component);
					Singleton<AudioManager>.Instance.PlaySound("sound_blink");
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", item.transform.position + new Vector3(0f, 0.7f, -0.003f), 3f);
				}
			}
			else if (state == State.Game2)
			{
				((BodyScannerDisplayerItem)displayer).PlayGame2Anim();
			}
		}

		private void SetState(State val)
		{
			state = val;
			if (state == State.Game1)
			{
				btnGame1.Find("Sprite").gameObject.SetActive(true);
				btnGame2.Find("Sprite").gameObject.SetActive(false);
			}
			else
			{
				btnGame1.Find("Sprite").gameObject.SetActive(false);
				btnGame2.Find("Sprite").gameObject.SetActive(true);
			}
			CheckScannerState();
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
